package com.gusto.movielens.recommendation.access;

import java.util.ArrayList;
import java.util.List;

import com.gusto.engine.colfil.Distance;
import com.gusto.engine.recommend.delagate.SemNeighborhoodDelegate;
import com.gusto.engine.semsim.Neighbor;
import com.gusto.engine.semsim.neighborhood.SemNeighborhoodService;

public class SemanticNeighborhoodDelegateImpl implements SemNeighborhoodDelegate {
	
	private SemNeighborhoodService semNeighborhoodService;
	public void setSemNeighborhoodService(SemNeighborhoodService semNeighborhoodService) {
		this.semNeighborhoodService = semNeighborhoodService;
	}
	
	public SemanticNeighborhoodDelegateImpl() {
		super();
	}
	
	public List<Distance> getNearItems(long itemId, Integer MAX_NEIGHBORS, Double MIN_SIMILARITY) {
		List<Distance> distances = new ArrayList<Distance>();
		List<Neighbor> neighbors = semNeighborhoodService.getNeighborhood("movie", ""+itemId, MIN_SIMILARITY, MAX_NEIGHBORS);
		for (Neighbor neighbor : neighbors) {
			Distance dist = new Distance();
			dist.setId1(itemId);
			dist.setId2(Long.parseLong(neighbor.getNeighbor()));
			dist.setDistance(neighbor.getSimilarity());
			dist.setCount(1);
			distances.add(dist); 
		}
		return distances;
	}
	
	public List<Distance> getNearUsers(long userId, Integer MAX_NEIGHBORS, Double MIN_SIMILARITY) {
		List<Distance> distances = new ArrayList<Distance>();
		List<Neighbor> neighbors = semNeighborhoodService.getNeighborhood("user", ""+userId, MIN_SIMILARITY, MAX_NEIGHBORS);
		for (Neighbor neighbor : neighbors) {
			Distance dist = new Distance();
			dist.setId1(userId);
			dist.setId2(Long.parseLong(neighbor.getNeighbor()));
			dist.setDistance(neighbor.getSimilarity());
			dist.setCount(1);
			distances.add(dist);
		}
		return distances;
	}

}
